var express = require('express');
var router = express.Router();
const {SuccessModel, ErrorModel} = require('../model/resModel.js')
const {login} = require('../controller/user.js')
/* GET users listing. */
router.post('/login', (req, res, next)=> {
  const {username, password} = req.body
  const result = login(username, password)
  return result.then(data => {
    if (data.username) {
      req.session.username = data.username
      req.session.realname = data.realname
      res.json(new SuccessModel('登陆成功'))
      return
    }
    res.json( new ErrorModel('登录失败'))
  })
});
router.get('/test',(req, res, next)=>{
  console.log('username',req.session.username)
  if (req.session.username){
    res.json(
      new SuccessModel('成功')
    )
    return
  }
  res.json(
    new ErrorModel('失败')
  )
})
module.exports = router;
